# -*- coding: utf-8 -*-

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import pymysql
pymysql.install_as_MySQLdb()

from meiriyitu.config import Config


db = SQLAlchemy()
migrate = Migrate()


from meiriyitu.route import index, message, write_content, get_userinfo, select_content, show_image, \
     jscode, get_openid, get_userid, add_content_and_up_image, beian, uploadimage, top_images, write_calendar_memo, \
     read_calendar_memo
# from spider.run_spider import run

def create_api():
    api = Flask(__name__)
    api.config.from_object(Config)
    # run()
    db.init_app(api)
    migrate.init_app(api, db)
    api.add_url_rule('/', 'beian', beian)
    api.add_url_rule('/index', 'index', index)
    api.add_url_rule('/jscode', 'jscode', jscode, methods=['POST', 'GET'])
    api.add_url_rule('/message', 'message', message, methods=['POST', 'GET'])
    api.add_url_rule('/write', 'write_content', write_content, methods=['POST'])
    api.add_url_rule('/userid', 'userid', jscode, methods=['GET'])
    api.add_url_rule('/openid', 'openid', get_openid, methods=['GET', 'POST'])
    api.add_url_rule('/userinfo', 'userinfo', get_userinfo, methods=['POST'])
    api.add_url_rule('/content', 'content', select_content, methods=['GET', 'POST'])
    api.add_url_rule('/upimage', 'upimage', add_content_and_up_image, methods=['POST'])
    api.add_url_rule('/showimages', 'showimages', top_images)
    api.add_url_rule('/image/<image>', 'images', show_image)
    api.add_url_rule('/upimage/<image>', 'uploadimage', uploadimage)
    api.add_url_rule('/writememo', 'writememo', write_calendar_memo, methods=['POST'])
    api.add_url_rule('/readmemo', 'readmemo', read_calendar_memo, methods=['POST'])

    return api
